home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 2,801 to 2,900 / aol-file-protocol-4400-2801-to-2900.zip / AOLDLs / C++ Files Library / Graphic Gems I, II & III (C_C++) / Graphics Gems C Code.sea / GemsIII / pl2plane.c < prev    next >
Text File  |  1992-06-16  |  3KB  |  89 lines

  1. /******************************************************************************
  2.                        Plane-to-Plane Intersection
  3.                        Author: Priamos Georgiades
  4. ******************************************************************************/
  5. #include <stdio.h>
  6. #include <math.h>
  7.  
  8. #define TRUE 1
  9. #define FALSE 0
  10. #define MRG_ZERO 1.0e-8
  11.  
  12. typedef struct vect3str {
  13.     float x, y, z;
  14.     } vect3;
  15. typedef struct vect4str {
  16.     float x, y, z, w;
  17.     } vect4;
  18.  
  19. #define Vect3Init(a, b, c, v) { \
  20.     (v).x = a; (v).y = b; (v).z = c; \
  21.     }
  22. #define Vect3Muls(s, v1, v2) { \
  23.     (v2).x = s * (v1).x; \
  24.     (v2).y = s * (v1).y; \
  25.     (v2).z = s * (v1).z; \
  26.     }
  27. #define Vect3Cross(v1, v2, v3) { \
  28.     (v3).x = (v1).y * (v2).z - (v1).z * (v2).y; \
  29.     (v3).y = (v1).z * (v2).x - (v1).x * (v2).z; \
  30.     (v3).z = (v1).x * (v2).y - (v1).y * (v2).x; \
  31.     }
  32.  
  33. /*
  34. Calculate the line of intersection between two planes. The two planes are
  35. specified by their equations in the form
  36.               P->x * X + P->y * Y + P->z * Z + P->w = 0.
  37. Initialize the unit direction vector of the line of intersection in xdir.
  38. Pick the point on the line of intersection on the coordinate plane most normal
  39. to xdir. Return TRUE if successful, FALSE otherwise (indicating that the planes
  40. don't intersect). The order in which the planes are given determines the choice
  41. of direction of xdir.
  42. */
  43. int GetXLine(vect4 *pl1, vect4 *pl2, vect3 *xdir, vect3 *xpt)
  44. {
  45. float invdet;  /* inverse of 2x2 matrix determinant */
  46. vect3 dir2;    /* holds the squares of the coordinates of xdir */
  47.  
  48. Vect3Cross(*pl1, *pl2, *xdir)
  49. dir2.x = xdir->x * xdir->x;
  50. dir2.y = xdir->y * xdir->y;
  51. dir2.z = xdir->z * xdir->z;
  52.  
  53.  
  54. if (dir2.z > dir2.y && dir2.z > dir2.x && dir2.z > MRG_ZERO)
  55.     {
  56.     /* then get a point on the XY plane */
  57.     invdet = 1.0 / xdir->z;
  58.     /* solve < pl1.x * xpt.x + pl1.y * xpt.y = - pl1.w >
  59.              < pl2.x * xpt.x + pl2.y * xpt.y = - pl2.w > */
  60.     Vect3Init(pl1->y * pl2->w - pl2->y * pl1->w,
  61.               pl2->x * pl1->w - pl1->x * pl2->w, 0.0, *xpt)
  62.     }
  63. else if (dir2.y > dir2.x && dir2.y > MRG_ZERO)
  64.     {
  65.     /* then get a point on the XZ plane */
  66.     invdet = 1.0 / xdir->y;
  67.     /* solve < pl1.x * xpt.x + pl1.z * xpt.z = -pl1.w >
  68.              < pl2.x * xpt.x + pl2.z * xpt.z = -pl2.w > */
  69.     Vect3Init(pl1->z * pl2->w - pl2->z * pl1->w, 0.0,
  70.               pl2->x * pl1->w - pl1->x * pl2->w, *xpt)
  71.     }
  72. else if (dir2.x > MRG_ZERO)
  73.     {
  74.     /* then get a point on the YZ plane */
  75.     invdet = 1.0 / xdir->x;
  76.     /* solve < pl1.y * xpt.y + pl1.z * xpt.z = - pl1.w >
  77.              < pl2.y * xpt.y + pl2.z * xpt.z = - pl2.w > */
  78.     Vect3Init(0.0, pl1->z * pl2->w - pl2->z * pl1->w,
  79.               pl2->y * pl1->w - pl1->y * pl2->w, *xpt)
  80.     }
  81. else /* xdir is zero, then no point of intersection exists */
  82.     return FALSE;
  83. Vect3Muls(invdet, *xpt, *xpt)
  84. invdet = 1.0 / (float)sqrt(dir2.x + dir2.y + dir2.z);
  85. Vect3Muls(invdet, *xdir, *xdir)
  86. return TRUE;
  87. }
  88.  
  89.